package com.qs.aspect;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qs.ConStants;
import com.qs.annotation.Log;
import com.qs.pojo.SysOperator;
import com.qs.pojo.SysUser;
import com.qs.utils.DateUtils;
import com.qss.service.OperatorService;
import com.qss.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * @Description
 * @user HP
 * @email 1460961774@qq.com
 * @Date 2021-10-08 11:52
 * @packageName com.qs.aspect
 */
@Component
@Aspect
public class LoggerAop {

    @Reference
    private OperatorService service;
    @Reference
    private UserService service1;
    //定义切入点
    @Pointcut("@annotation(com.qs.annotation.Log)")
    public void a(){

    }
    @AfterReturning(pointcut = "a()",returning = "result")
    public void afterRunning(JoinPoint point , Object result) throws JsonProcessingException {
        SysOperator sysOperator = new SysOperator();
        Object aThis = point.getThis();
        //该方法的所有参数
        Object[] args = point.getArgs();
        ObjectMapper json = new ObjectMapper();
        String string = json.writeValueAsString(args);
        sysOperator.setOperParam(string);
        System.out.println("******输入参数为"+string);
        Signature signature = point.getSignature();
        MethodSignature s= (MethodSignature) signature;
        Method method = s.getMethod();

        Log log = method.getAnnotation(Log.class);
        String moduleType = log.moduleType();
        String moduleName = log.moduleName();
        //包含Request的相关请求
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes attribute= (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = attribute.getRequest();
        int num =0;
        String method1 = request.getMethod();
        if ("POST".equals(method1.toUpperCase())){
            num=1;
        }
        if ("DEETE".equals(method1.toUpperCase())){
            num=2;
        }
        sysOperator.setModuleType(moduleType);
        sysOperator.setModuleName(moduleName);
        sysOperator.setType(num);
        Subject subject = SecurityUtils.getSubject();
        SysUser user1= (SysUser) request.getSession().getAttribute(ConStants.CURR_USER);
        SysUser user = service1.queryUserByUserName(user1.getLoginName());
        System.out.println("user = " + user);
        sysOperator.setUserId(user.getUserId());
        sysOperator.setOperAddress(user.getAddress());
        sysOperator.setOperDate(DateUtils.Date("yyyy-MM-dd HH:mm:ss"));
        sysOperator.setOperIp(request.getRemoteAddr());


        service.saveLogger(sysOperator);

        Class<?> aClass = aThis.getClass();
        System.out.println(aClass);
    }
}
